﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZRT.RHPX.Entity;
using ZRT.RHPX.Service;

namespace ZRT.RHPX.Web.Controllers
{
    public class AdminController : Controller
    {
        #region 属性

        public SysMenuService MenuService { get; private set; }

        #endregion 属性

        public AdminController()
        {
            MenuService = new SysMenuService();
        }

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        #region 左侧菜单

        /// <summary>
        /// 左侧菜单
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLeftMenuList()
        {
            var MenuList = MenuService.Query().ToList();
            var Model = from m in MenuList
                        where m.ParentID == null
                        select new
                        {
                            ID = m.ID,
                            text = m.MenuName,
                            FullName = m.MenuName,
                            NavigateUrl = m.NavigateUrl,
                            IconCss = m.IconCss,
                            state = "open",
                            children = GetLeftSonMenu(MenuList, m.ID)
                        };

            return Json(Model, JsonRequestBehavior.AllowGet);
        }

        private Object GetLeftSonMenu(List<SysMenu> AllMenu, int ParentID)
        {
            var Model = from m in AllMenu
                        where m.ParentID != null && m.ParentID == ParentID
                        select new
                        {
                            ID = m.ID,
                            text = m.MenuName,
                            FullName = m.MenuName,
                            NavigateUrl = m.NavigateUrl,
                            IconCss = m.IconCss,
                            state = "open",
                            //children = GetLeftSonMenu(AllMenu, m.ID) //不需要递归
                        };
            return Model;
        }

        #endregion 左侧菜单

        #region 菜单管理

        /// <summary>
        /// 菜单列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public ActionResult MenuList()
        {

            return View();
        }

        public ActionResult GetMenuListData()
        {
            var MenuList = MenuService.Query().ToList();
            var Model = from m in MenuList
                        where m.ParentID == null
                        select new
                        {
                            ID = m.ID,
                            MenuName = m.MenuName,
                            MenuDesc = m.MenuDesc ?? "",
                            IconCss = m.IconCss ?? "",
                            NavigateUrl = m.NavigateUrl ?? "",
                            SeqNum = m.SeqNum,
                            IsEnable = m.IsEnable,
                            MenuCategory = m.MenuCategory,
                            children = GetSonMenu(MenuList, m.ID)
                        };

            return Json(Model, JsonRequestBehavior.AllowGet);
        }

        private Object GetSonMenu(List<SysMenu> AllMenu, int ParentID)
        {
            var Model = from m in AllMenu
                        where m.ParentID != null && m.ParentID == ParentID
                        select new
                        {
                            ID = m.ID,
                            MenuName = m.MenuName,
                            MenuDesc = m.MenuDesc ?? "",
                            IconCss = m.IconCss ?? "",
                            NavigateUrl = m.NavigateUrl ?? "",
                            SeqNum = m.SeqNum,
                            IsEnable = m.IsEnable,
                            MenuCategory = m.MenuCategory,
                            children = GetSonMenu(AllMenu, m.ID) //递归
                        };
            return Model;
        }

        public ActionResult GetMenuTreeData()
        {
            var MenuList = MenuService.Query().ToList();
            var Model = from m in MenuList
                        where m.ParentID == null
                        select new
                        {
                            id = m.ID,
                            text = m.MenuName,
                            children = GetSonMenuTree(MenuList, m.ID)
                        };

            return Json(Model, JsonRequestBehavior.AllowGet);
        }

        private Object GetSonMenuTree(List<SysMenu> AllMenu, int ParentID)
        {
            var Model = from m in AllMenu
                        where m.ParentID != null && m.ParentID == ParentID
                        select new
                        {
                            id = m.ID,
                            text = m.MenuName,
                            children = GetSonMenuTree(AllMenu, m.ID) //递归
                        };
            return Model;
        }

        [HttpGet]
        public ActionResult MenuAdd()
        {
            return View();
        }

        [HttpPost]
        public ActionResult MenuAdd(SysMenu Entity)
        {
            return Content("OK");
        }

        #endregion 菜单管理

        #region 新闻管理

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public ActionResult NewsList(int PageIndex = 1, int PageSize = 10)
        {
            return View();
        }

        #endregion 新闻管理
    }
}
